package com.chatdating.controller;

import com.chatdating.annotate.IsLogin;
import com.chatdating.entity.ChatMessage;
import com.chatdating.entity.pojo.ChatMessages;
import com.chatdating.entity.pojo.Result;
import com.chatdating.entity.pojo.SendMessage;
import com.chatdating.entity.vo.LoginInVo;
import com.chatdating.mapper.ChatMessageMapper;
import com.chatdating.redis.RedisComponet;
import com.chatdating.service.ChatMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/chat")
public class ChatControlle {
    private static final Logger logger = LoggerFactory.getLogger(AuthController.class);
    @Autowired
    private RedisComponet redisComponet;

    @Autowired
    private ChatMessageMapper chatMessageMapper;

    @Autowired
    private ChatMessageService chatMessageService;
    /**
     * 查询某个联系人的聊天记录
     * @param request
     * @return
     */
    @IsLogin(checkLogin = true)
    @GetMapping("messages")
    public Result hello(HttpServletRequest request,
                        @RequestParam String sessionId){

        List<ChatMessage> chatMessages = chatMessageMapper.queryChatMessage(sessionId);
        return Result.success(chatMessages);
    }



    @RequestMapping("/sendMessage")
    @IsLogin
    public Result sendMessage(
            HttpServletRequest request,
            @RequestBody SendMessage SendMessage){
        String tockens = tockens(request);LoginInVo tokenUserInfoDto = redisComponet.getTokenUserInfoDto(tockens);
        String username = tokenUserInfoDto.getUsername();
        chatMessageService.saveMessage(SendMessage,username);
        return Result.success();
    }

    public String tockens(HttpServletRequest request){
        String authHeader = request.getHeader("Authorization");
        String token = authHeader.substring(7);
        return token;
    }

}

